package com.achobeta.recruitment.utils;

import com.achobeta.recruitment.common.AppResult;
import com.achobeta.recruitment.common.ResultCode;
import com.achobeta.recruitment.config.AppConfig;
import com.achobeta.recruitment.config.QRCodeConstant;
import com.achobeta.recruitment.exception.ApplicationException;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-30
 * Time: 15:53
 */
@Slf4j
public class CheckUtils {

    public static void printWarn(ResultCode resultCode) {
        log.warn(resultCode.toString());
        throw new ApplicationException(resultCode);
    }

    public static void checkResult(String result, ResultCode resultCode) {
        String errcode = JSONObject.parseObject(result).getString("errcode");
        if(errcode != null && !errcode.equals("0")) {
            printWarn(resultCode);
        }
    }

    public static void checkRows(int rows, int realRows, ResultCode resultCode) {
        if(rows != realRows) {
            printWarn(resultCode);
        }
    }

    public static void checkStringParams(ResultCode resultCode, String... params) {//可变参数必须在最后面，因此只有一个，这样很合理，可以判断前面的几个必传的参数无误
        if(!StringUtils.hasLength(params)) {
            printWarn(resultCode);
        }
    }


    /**
     * 只要有一个为null，就错
     * @param objects
     */
    public static void checkNULL(Object... objects) {
        if(!Arrays.stream(objects).allMatch(o -> o != null)) {
            printWarn(ResultCode.ERROR_IS_NULL);
        }
    }


    /**
     * 检查文件类型是否正确
     * @param type
     */
    public static void checkImage(String type) {
        String[] types = {".png", ".jpg", ".jpeg", ".gif"};
        if(!Arrays.asList(types).contains(type)) {
            printWarn(ResultCode.FAILED_FILE_TYPE);
        }
    }

    public static void checkFileExists(String fileName) {
        checkStringParams(ResultCode.FAILED_PARAMS_VALIDATE, fileName);
        if(!new File(AppConfig.ROOT + fileName).exists()) {
            printWarn(ResultCode.FAILED_NOT_EXISTS);
        }
    }
    private static boolean isUrlAccessible(String urlString) {
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            int responseCode = connection.getResponseCode();
            return responseCode == 200; // 如果状态码为 200，则返回 true，表示可以访问
        } catch (IOException e) {
            return false; // 发生异常时，返回 false，表示不可访问
        }
    }
    public static void checkURL(String url) {
        checkStringParams(ResultCode.FAILED_PARAMS_VALIDATE, url);
        if(!isUrlAccessible(url)) {
            printWarn(ResultCode.FAILED_URL_NOT_EXIT);
        }
    }

}
